//HandleFieldFill.java
package com.atyouqidian.mybatisp.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ProjectName: mybatisp
 * @ClassName: HandleFieldFill
 * @Description: 设置属性自动填充， 前提是 类中的属性需要加注解 ： @TableField(fill = FieldFill.XXX)
 * @Author: www.atyouqidian.com
 * @UpdateDate: 2021-08-30 0:40
 * @UpdateRemark: 更新
 * @Version: 1.0
 */
@Component
public class HandleFieldFill implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);

        // 乐观锁使用的版本号
        this.setFieldValByName("version", 1, metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
